/*
 * Copyright (c) 2014, Mentor Graphics Corporation
 * Copyright (c) 2015 Xilinx, Inc.
 * Copyright (c) 2016 Freescale Semiconductor, Inc.
 * Copyright 2016 NXP
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef RPMSG_NS_H_
#define RPMSG_NS_H_

#include "rpmsg_lite.h"

#define RL_NS_EPT_ADDR (0x35u)

/* 最多可提供32个标志 */
enum rpmsg_ns_flags {
    RL_NS_CREATE  = 0,
    RL_NS_DESTROY = 1,
};

/* 新的端点名称服务回调函数类型. */
typedef void (*rpmsg_ns_new_ept_cb)(uint32_t new_ept, const char *new_ept_name,
                                    uint32_t flags, void *user_data);

/* 名称服务回调数据结构. */
struct rpmsg_ns_callback_data {
    rpmsg_ns_new_ept_cb cb;
    void *user_data;
};

/* 名称服务上下文结构. */
struct rpmsg_ns_context {
    struct rpmsg_lite_endpoint *ept;
    struct rpmsg_ns_callback_data *cb_ctxt;
};

typedef struct rpmsg_ns_context *rpmsg_ns_handle;

/* 名称服务静态上下文结构. */
struct rpmsg_ns_static_context_container {
    struct rpmsg_lite_ept_static_context ept_ctxt;
    struct rpmsg_ns_callback_data cb_ctxt;
    struct rpmsg_ns_context ns_ctxt;
};

typedef struct rpmsg_ns_static_context_container rpmsg_ns_static_context;

#if defined(__cplusplus)
extern "C" {
#endif

/*******************************************************************************
 * API
 ******************************************************************************/

/* 导出API函数 */

/**
 * @brief  注册应用程序名称服务回调.
 * @param  rpmsg_lite_dev RPMsg-Lite实例.
 * @param  app_cb 应用程序名称服务回调函数.
 * @param  user_data 应用程序名称服务回调函数参数.
 * @return 成功返回名称服务句柄, 失败返回RL_NULL.
 */
#if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
rpmsg_ns_handle rpmsg_ns_bind(struct rpmsg_lite_instance *rpmsg_lite_dev,
    rpmsg_ns_new_ept_cb app_cb, void *user_data,
    rpmsg_ns_static_context *ns_ept_ctxt);
#else
rpmsg_ns_handle rpmsg_ns_bind(struct rpmsg_lite_instance *rpmsg_lite_dev,
    rpmsg_ns_new_ept_cb app_cb, void *user_data);
#endif /* RL_USE_STATIC_API */

/**
 * @brief  注销应用程序名称服务回调并清理.
 * @param  rpmsg_lite_dev RPMsg-Lite实例.
 * @param  handle 名称服务句柄.
 * @return 返回函数执行状态, 成功时返回RL_SUCCESS.
 */
int32_t rpmsg_ns_unbind(struct rpmsg_lite_instance *rpmsg_lite_dev,
    rpmsg_ns_handle handle);

/**
 * @brief  向远端设备发送名称服务通知.
 * @param  rpmsg_lite_dev RPMsg-Lite实例.
 * @param  new_ept 要通知的新端点.
 * @param  ept_name 要通知的新端点的名称.
 * @param  flags 通道创建/删除标志.
 * @return 返回函数执行状态, 成功时返回RL_SUCCESS.
 */
int32_t rpmsg_ns_announce(struct rpmsg_lite_instance *rpmsg_lite_dev,
    struct rpmsg_lite_endpoint *new_ept, const char *ept_name, uint32_t flags);

#if defined(__cplusplus)
}
#endif

#endif /* RPMSG_NS_H_ */
